package com.msa.msaportal.webservice;


import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.msa.msaportal.entity.PortletViewRes;
import com.msa.msaportal.entity.UserComponent;
import com.msa.msaportal.entity.UserTile;
import com.msa.msaportal.service.IPortaletViewResService;
import com.msa.msaportal.service.IUserComponentService;
import com.msa.msaportal.service.IUserTileService;

public class UserTileServicesImpl implements UserTileServices {
	@Resource(name="userTileManager")
	private IUserTileService userTileManager;

	@Resource(name="userComponentManager")
	private IUserComponentService userComponentManager;
	@Override
	public String getUserModules(String userId) {
		try {
			UserTile userTile = userTileManager.getUserModules(userId);
			if (userTile != null) {
				return JSONObject.fromObject(userTile).toString();
			} else {
				return new JSONObject().toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new JSONObject().toString();
			// TODO: handle exception
		}
		
	}

	@Override
	public String changeMySubModules(String jsonParameter) {
		try {
			String userId = JSONObject.fromObject(jsonParameter).getString(
					"userId");
			String moduleIds = JSONObject.fromObject(jsonParameter)
					.getString("moduleIds");
			UserTile userTile = userTileManager.getUserModules(userId);
			if (userTile != null) {
				System.out.println("not null");
				userTile.setMODULE_IDS(moduleIds);
				System.out.println(userTile.getMODULE_IDS()+"########");
				userTileManager.updateUserModules(userTile);
			} else {
				System.out.println("null");
				userTileManager.insertUserModules(userId, moduleIds);
			}
			return JSONObject.fromObject("{'result':'1','info':'修改成功'}").toString();
		} catch (Exception e) {
			e.printStackTrace();
			return JSONObject.fromObject("{'result':'2','info':'接口异常!'}").toString();
		}
	}

	@Override
	public String getUserComponents(String userId) {
		try {
			List<UserComponent> userComponents = userComponentManager.getUserComponents(userId);
			return JSONArray.fromObject(userComponents).toString();
		} catch (Exception e) {
			e.printStackTrace();
			return new JSONArray().toString();
			// TODO: handle exception
		}
	}
}
